<?php 
namespace app\superadmin\controller;
use think\Db;
use think\Controller;
class Video extends Base{
	public function index(){
		$videos=Db::name('videos v')->join("videotype a","v.tid=a.tid")->where("uploadadmin='0'")->join('occupation o','v.oid=o.oid')->paginate(10);
		$this->assign('videos',$videos);

     //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
    //视频类型
    $types=Db::name('videotype')->where("tstatus='0'")->select();

    //知识体系
    $knows=Db::name('knowledge')->where("kstatus='0'")->select();
    $this->assign("knows",$knows);

      //职业方向
    $occus=Db::name('occupation')->where("ostate='0'")->select();
    $this->assign("occus",$occus);

    $this->assign('types',$types);
		return $this->fetch();
	}


  //ajax的知识体系
  public function show(){
    $know=input('know');

    $kboys=Db::name("knowbok")->where('kid',$know)->select();
    // dump($kboys);
    $s="";
    foreach($kboys as $v){
      $s.="<option value=\"{$v['kbid']}\">{$v['kbname']}</option>";  
    }
    echo $s;
  }




		  //屏蔽/解除屏蔽
	public function shield($vid){

  $ustatenum=Db::name("videos")->where("vid='$vid'")->value('vstatus');

  if($ustatenum==0){
    if (Db::name("videos")->where("vid='$vid'")->update(['vstatus'=>1])) {
      $this->redirect('index');
    }else{
      $this->error('解除屏蔽失败');
    }

  }
  else{
    if (Db::name("videos")->where("vid='$vid'")->update(['vstatus'=>0])) {
      $this->redirect('index');
    }else{
      $this->error('屏蔽失败');
    }    
  }


	}
  //添加视频
  public function addvideo(){
    $data=input("post.");
    
    unset($data["know"]);
    // dump($data);
    $file=request()->file("pic");
    // dump($file);
    $picname="";
    if ($file) {
      # code...
      $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'videopic');
      if ($info) {
        # code...
        $picname=$info->getFilename();

      }else{
        $this->error($file->getErrot());
      }
    }
    $data+=['pic'=>$picname,
            'uploaddate'=>date('Y-m-d'),
            'uploadadmin'=>0,
            ];
            // dump($data);
    $res=Db::name('videos')->insert($data);
    if ($res) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('视频添加失败！');
    }
  }
  //修改视频信息界面
  public function update($vid){
    $videoupdates=Db::name('videos v')->join('videotype t','v.tid=t.tid')->join('knowbok kb','v.kbid=kb.kbid')->join('knowledge k','kb.kid=k.kid')->join('occupation o','v.oid=o.oid')->where('vid',$vid)->select();
    $this->assign('videoupdates',$videoupdates);
    //知识体系
     $knows=Db::name('knowledge')->where("kstatus='0'")->select();
    $this->assign("knows",$knows);
    //职业方向
     $occs=Db::name('occupation')->where("ostate='0'")->select();
    $this->assign("occs",$occs);
    //视频类型
    $types=Db::name('videotype')->select();
    $this->assign('types',$types);
    return $this->fetch();
  }
    //更新用户信息
  public function doupdate($vid){
    // dump($sid);
    $data=input("post.");
     unset($data["know"]);
     // unset($data["occu"]);
     // dump($data);
    $file = request()->file('pic');
     // dump($files);
    // dump($file);
    $picname="";
    if ($file==null) {
      # code...
       $row=Db::name('videos')->where('vid',$vid)->select();
      $file=$row[0]['pic'];
      $picname=$row[0]['pic'];
       // dump($row[0]['spic']);
    }else{
     
       // if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'videopic');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }
        
        $row1=Db::name('videos')->where('vid',$vid)->select();
        unlink('../public/static/videopic/'.$row1[0]['pic']);

     
      
    }
     $data+=['pic'=>$picname,
              ];
    $up=Db::name('videos')->where('vid',$vid)->update($data);
    if ($up) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('请至少修改一条信息');
    }
  }

  //视频详情页
  public function videodetails($vid){
    $videoDetails=Db::name('videos')->alias('v')->join('videotype t','v.tid=t.tid')->join('knowbok k','v.kbid=k.kbid')->join('knowledge l','k.kid=l.kid') ->join('occupation o','v.oid=o.oid')->where('vid',$vid)->select();

    //子视频目录
    $videochilds=Db::name('videochild')->where('vid',$vid)->paginate(10);
    $this->assign('videoDetails',$videoDetails);
    $this->assign('videochilds',$videochilds);
     //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
    return $this->fetch();
  }
  //删除子视频
  public function delate($vid,$cvid){
   
    $res=Db::name('videochild')->where('cvid',$cvid)->select();
    // dump($res[0]);
    unlink('../public/static/videochild/'.$res[0]["cvname"]);
     $red=Db::name('videochild')->where('cvid',$cvid)->delete();
    if ($red) {
      $this->redirect("videodetails",["vid"=>$vid]);
    }else{
      $this->error('删除失败！！');
    }
  }

  //子视频播放页
  public function videochild($cvid){
    $childs=Db::name('videochild')->where('cvid',$cvid)->select();
    $this->assign('childs',$childs);
    return $this->fetch();
  }

  //添加子视频页
  public function addvideochild($vid){
    $data=input("post.");
    // dump($data);
     $file = request()->file("cvname");
     $picname="";
     // dump($picname);
     dump($file);
      $info=$file->rule('uniqid')->validate(['size'=>15242880,'ext'=>'mp4'])->move(ROOT_PATH.'public'.DS.'static'.DS.'videochild');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }
        // dump($picname);
        $data+=['cvname'=>$picname,
                'cvtime'=>date("Y-m-d")];
        $res=Db::name('videochild')->where('vid',$vid)->insert($data);
        if ($res) {
          # code...
           $this->redirect("videodetails",["vid"=>$vid]);
        }else{
          $this->error('添加子视频失败！');
        }
  }

}